package com.book.functional;

import java.io.File;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;

public class InitDB {
	private static Log log = LogFactory.getLog(InitDB.class);

	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		Properties jdbcProp = new Properties();
		jdbcProp.load(InitDB.class.getResourceAsStream("/jdbc.properties"));

		log.info("正在插入数据......");
		File buildFile = new File("build-initdb.xml");
		Project prj = new Project();
		prj.init();
		ProjectHelper helper = ProjectHelper.getProjectHelper();
		helper.parse(prj, buildFile);

		prj.setProperty("driver", jdbcProp.getProperty("jdbc.driverClassName"));
		prj.setProperty("url", jdbcProp.getProperty("jdbc.url"));
		prj.setProperty("username", jdbcProp.getProperty("jdbc.username"));
		prj.setProperty("password", jdbcProp.getProperty("jdbc.password"));

		DefaultLogger buildLogger = new DefaultLogger();
		buildLogger.setErrorPrintStream(System.err);
		buildLogger.setOutputPrintStream(System.out);
		buildLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
		prj.addBuildListener(buildLogger);

		prj.executeTarget(prj.getDefaultTarget());

		log.info("数据插入已完成,用时：" + (System.currentTimeMillis() - start) / 1000 + "秒.");
	}

}
